package com.kehutong.charge.service.impl;

import com.kehutong.charge.entity.ChargeBillInterim;
import com.kehutong.charge.entity.ChargeTypeTemp;
import com.kehutong.charge.entity.MeterClassify;
import com.kehutong.charge.entity.MeterManage;
import com.kehutong.charge.service.ChargeTypeTempService;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: kht
 * @description: ChargeTypeTempServiceImpl
 * @author: joneron
 * @create: 2021/6/23
 **/
public class ChargeTypeTempServiceImpl implements ChargeTypeTempService {

    private static final Logger logger = LoggerFactory.getLogger(ChargeTypeTempServiceImpl.class);

    @Inject
    private JdbcSession jdbcSession;

    @Override
    public Page<ChargeTypeTemp> findPage(JSONObject jsonObject) {
        FindPage<ChargeTypeTemp> find = jdbcSession.findPage(ChargeTypeTemp.class)
                .eq("deleted", false)
                .like(jsonObject,"feeTypeName")
                .eq(jsonObject,"status")
                .order("sort", "desc")
                .order("createTime", "desc");
        Page<ChargeTypeTemp> page = find.page(jsonObject).exe();

        List<ChargeTypeTemp> list = page.getList();
        if(Objects.isEmpty(list)){
            return page;
        }

        Set<String> feeNameSet = list.stream().map(ChargeTypeTemp::getFeeTypeName).collect(Collectors.toSet());
        List<ChargeBillInterim> interimList = jdbcSession.findArray(ChargeBillInterim.class)
                .eq("deleted", false)
                .in("feeTypeName", feeNameSet)
                .exe();
        if(Objects.isEmpty(interimList)){
            return page;
        }
        list.stream().map(l->{
            long count = interimList.stream().filter(i -> i.getFeeTypeName().equals(l.getFeeTypeName())).count();
            l.setNumber(count);
            return l;
        }).collect(Collectors.toList());

        return page;
    }

    @Override
    public List<ChargeTypeTemp> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargeTypeTemp.class)
                .eq("deleted", false)
                .eq("status", true)
                .exe();
    }

    @Override
    public Object save(ChargeTypeTemp chargeTypeTemp) {
        return jdbcSession.insert(chargeTypeTemp);
    }

    @Override
    public Object update(ChargeTypeTemp chargeTypeTemp) {
        return jdbcSession.updateById(chargeTypeTemp);
    }

    @Override
    public Object deleteById(String id) {
        ChargeTypeTemp result = Pool.get(ChargeTypeTemp.class, id);
        if (result != null) {
            result.setDeleted(true);
            jdbcSession.updateById(result);
        }
        return 1;
    }
}
